//
//  XTUtility+UserAgent.m
//  XTUtilityKit
//
//  Created by Tong on 2017/6/2.
//  Copyright © 2017年 Tong. All rights reserved.
//

#import "XTUtility+UserAgent.h"
#import "XTUtility+Device.h"
#include <sys/utsname.h>

@implementation XTUtility (UserAgent)

/** 修改用户代理属性 */
+ (void)initializeUserAgentWithSomeParameter
{
    UIWebView *testWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  
    NSString *userAgent = [testWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSString *appId     = [infoDictionary objectForKey:@"ApplicationID"];
    NSString *bundleId  = [infoDictionary objectForKey:@"CFBundleIdentifier"];
    NSString *channelNo = [infoDictionary objectForKey:@"ChannelNo"];
    NSString *version   = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *idfa      = [self getDeviceIdfa];

    /** 获取相关硬件信息 */
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platformText = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    /** 修改UserAgent */
    NSString *newUserAgent = [NSString stringWithFormat:@"%@-feng{iOSMobileClient;%@;%@;%@;%@;%@;%@}",userAgent,platformText,appId,channelNo,bundleId,version,idfa];
    
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent, @"User-Agent":newUserAgent}];
}

@end
